package MyApp::Workflow::Action::AgreeCompany;

use strict;
use base qw( Workflow::Action );
use Workflow::Exception qw( persist_error );
use Workflow::Factory qw( FACTORY );

sub execute {
    my ($self,$wf) = @_;
    my $db=$wf->context->param('db');

    my $issue_id = $db->query('select number from daiban where workflow_id=? AND workflow_type=1',
                              $wf->id)->array->[0];
    my $company_hashes = $db->query('select * from company_application where issue_id=?',
                                     $issue_id)->hashes;
    foreach my $hash (@$company_hashes) {
        if ($hash->{operation} == 1) {
            #判断该公司是否以前注册过：
            my $companyname = $db->query('select name from companylist where id=?',$hash->{id})->array;

            if ($companyname->[0]) { #如果存在公司名称
                $db->query('update companylist set name=?,address=?,tel=?,enable=1,issue_id=? where id=?',
                            $hash->{name},$hash->{address},$hash->{tel},$hash->{issue_id},$hash->{id});
                
            } else {
                $db->query('insert into companylist (id,name,address,tel,enable,issue_id) values (?,?,?,?,1,?)',
                            $hash->{id},$hash->{name},$hash->{address},$hash->{tel},$hash->{issue_id});
            }

            my $relation_number = $db->query('select number from sbu_company_relation where company_id=? and sbu=? and role=?',$hash->{id},$hash->{sbu},$hash->{role})->array;
  
            $db->query('insert into sbu_company_relation (company_id,sbu,role,enable) values (?,?,?,1)',
                        $hash->{id},$hash->{sbu},$hash->{role}) if (not $relation_number->[0]);
        } else {
            #当公司信息注销时发生的动作
            
            
            #增加判断该公司信息是否多事业部：

            my $relation_array = $db->query('select sbu from sbu_company_relation where company_id=?',
                                            $hash->{id})->arrays;
            
            if($relation_array->[1]->[0]) {

                #获取管理员账户信息——账户名
                my $admin_list = $db->query('select username from admin_account where sbu_id=?',$hash->{sbu})->arrays;
                my @adminlist;
                for my $adminname (@$admin_list) {
                    $adminname = $adminname->[0];
                    push @adminlist,$adminname;
                }

                #获取账户
                my $array = $db->query('select username,password,tibaoren from vpn_account where company_id=?',
                                      $hash->{id})->hashes;

                #将数据放入账户处理表
                eval{
                    my $tx = $db->begin;
                    foreach my $username (@$array) {

                        #增加判断是否属于该事业部

                        my $tibaoren = $username->{tibaoren};

                        next if ( ! grep /^$tibaoren$/, @adminlist );

                        $db->query('insert into account_dealing (username,company_name,password,operation) values (?,?,?,0)',$username->{username},$hash->{name},$username->{password});
                    }
                    $tx->commit;
                };
                print $@ if $@;

                #获取mac地址
                my $array02 = $db->query('select mac,tibaoren from maclist where company=?',
                                        $hash->{id})->hashes;

                #将数据放入MAC地址处理表
                eval{
                    my $tx = $db->begin;
                    foreach my $mac (@$array02) {

                        #增加判断是否属于该事业部

                        my $tibaoren = $mac->{tibaoren};

                        next if ( ! grep /^$tibaoren$/, @adminlist );

                        $db->query('insert into mac_dealing (mac,operation) values (?,0)',$mac->{mac});
                    }
                    $tx->commit;
                };
                print $@ if $@;



            }else{

              #公司信息表enable字段置0

              $db->query('update companylist set enable=0 where id=?', $hash->{id});
              
              #要注销的账户
              my $array = $db->query('select username,password from vpn_account where company_id=?',
                                      $hash->{id})->hashes;
            
              #将数据放入账户处理表
              eval{
                  my $tx = $db->begin;
                  foreach my $username (@$array) {
                      $db->query('insert into account_dealing (username,company_name,password,operation) values (?,?,?,0)',$username->{username},$hash->{name},$username->{password});
                  }
                  $tx->commit;
              };
              print $@ if $@;
  
              #查询要注销的MAC地址
              my $array02 = $db->query('select mac from maclist where company=?',
                                        $hash->{id})->arrays;
              #将数据放入MAC地址处理表
              eval{
                  my $tx = $db->begin;
                  foreach my $mac (@$array02) {
                      $mac = $mac->[0];
                      $db->query('insert into mac_dealing (mac,operation) values (?,0)',$mac);
                  }
                  $tx->commit;
              };
              print $@ if $@;

            }
            #公司注销时所属的事业部关系表enable字段置0等待vpn配置文件处理完成后删除
            $db->query('update sbu_company_relation set enable=0 where company_id=? and sbu=?',$hash->{id},$hash->{sbu});
        }

        $db->query('update daiban set is_finish=1 where workflow_type=1 and workflow_id=?',$wf->id);
    }

    $wf->add_history(
        Workflow::History->new({
            action      => '审核',
            description => sprintf("同意!"),
            user        => $wf->context->param('current_user'),
            time_zone   => 'local'
        })
    );
};

1;
